﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

using Styx;
using Styx.Helpers;
using Styx.Loaders;
using Styx.Patchables;
using Styx.Plugins;
using Styx.Common;
using Styx.CommonBot;
using Styx.Pathing;
using Styx.CommonBot.Profiles;
using Styx.WoWInternals;
using Styx.WoWInternals.Misc;
using Styx.WoWInternals.World;
using Styx.WoWInternals.WoWCache;
using Styx.WoWInternals.WoWObjects;

using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;
using System.Windows.Media;

using AnimusPlugin;
using AnimusProfile;
using AnimusQB;

using ObjectManager = Styx.WoWInternals.ObjectManager;

namespace TheAnimus
{
	public class TheAnimus : HBPlugin
	{
		public bool hasBeenInitialized = false;
		public bool brodieupdated = false;
		
		#region Overrides except pulse
		
		public override string Author { get { return "TheBrodieMan"; } }
		public override Version Version { get { return new Version(2, 0, 5); } }
		public override string Name { get { return "The Animus (Premium)"; } }
		public override bool WantButton { get { return true; } }
		public override string ButtonText { get { return "Animus Interface"; } }
		public override void OnButtonPress()
		{
			bool isRunning = TreeRoot.IsRunning;
			if (isRunning)
			{
				MessageBox.Show("Bot is running, stop bot before initiating The Animus", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			}
			else
				needRestart();
		}
		private Animus_Plugin_Updater UpdaterPlugin;
		private Animus_QB_Updater UpdaterQB;
		private Animus_Profile_Updater UpdaterProfile;

		public override void Initialize()
		{
			if (!hasBeenInitialized)
			{
				Logging.Write(Colors.Teal, "Loaded The Animus (Public Version) v" + Version.ToString());
				if (StyxWoW.Me.Level < 60)
					Logging.Write(Colors.Red, "WARNING: [ANIMUS] is designed for level 60 and above ONLY!");
				Logging.Write(Colors.Teal, "Please Wait While [ANIMUS] Checks For Updates, This MAY Take Several Minutes");
				hasBeenInitialized = true;
				try
				{
					UpdaterPlugin = new Animus_Plugin_Updater("https://theanimusplus.googlecode.com/svn/trunk/", "TheAnimus");
					if (UpdaterPlugin.UpdateAvailable())
					{
						Logging.Write("[ANIMUS Plugin Updater] Update to $" + UpdaterPlugin.GetNewestRev().ToString() + " is available! You are on $" + UpdaterPlugin.CurrentRev.ToString());
						Logging.Write("[ANIMUS Plugin Updater] Starting update process...");
						if (UpdaterPlugin.Update())
						{
							Logging.Write("[ANIMUS Plugin Updater] Plugin is now up to date! HB Reload REQUIRED");
							brodieupdated = true;
						}
						else
							Logging.Write("[ANIMUS Plugin Updater] Error trying to auto-update. Please update manually!");
					}
					else
						Logging.Write("[ANIMUS Plugin Updater] is at Rev $" + UpdaterPlugin.CurrentRev.ToString() + " and is up to date!");
				}
				catch (Exception ex)
				{
					Logging.Write(Colors.Teal, "Unable to run [ANIMUS] update process");
					Logging.Write(LogLevel.Diagnostic, "[ANIMUS]: Exception " + ex.Message);
				}
				try
				{
					UpdaterQB = new Animus_QB_Updater("https://tbmpqb.googlecode.com/svn/trunk/", "");
					if (UpdaterQB.UpdateAvailable())
					{
						Logging.Write("[ANIMUS QB Updater] Update to $" + UpdaterQB.GetNewestRev().ToString() + " is available! You are on $" + UpdaterQB.CurrentRev.ToString());
						Logging.Write("[ANIMUS QB Updater] Starting update process...");
						if (UpdaterQB.Update())
						{
							Logging.Write("[ANIMUS QB Updater] QBs are now up to date! HB Reload REQUIRED");
							brodieupdated = true;
						}
						else
							Logging.Write("[ANIMUS QB Updater] Error trying to auto-update. Please update manually!");
					}
					else
						Logging.Write("[ANIMUS QB Updater] is at Rev $" + UpdaterQB.CurrentRev.ToString() + " and is up to date!");
				}
				catch (Exception ex)
				{
					Logging.Write(Colors.Teal, "Unable to run [ANIMUS QB Updater] update process");
					Logging.Write(LogLevel.Diagnostic, "[ANIMUS QB Updater]: Exception " + ex.Message);
				}
				try
				{
					UpdaterProfile = new Animus_Profile_Updater("https://animusplusprofiles.googlecode.com/svn/trunk/", "");
					if (UpdaterProfile.UpdateAvailable())
					{
						Logging.Write("[ANIMUS Profile Updater] Update to $" + UpdaterProfile.GetNewestRev().ToString() + " is available! You are on $" + UpdaterProfile.CurrentRev.ToString());
						Logging.Write("[ANIMUS Profile Updater] Starting update process...");
						if (UpdaterProfile.Update())
							Logging.Write("[ANIMUS Profile Updater] Profiles are now up to date!");
						else
							Logging.Write("[ANIMUS Profile Updater] Error trying to auto-update. Please update manually!");
					}
					else
						Logging.Write("[ANIMUS Profile Updater] is at Rev $" + UpdaterProfile.CurrentRev.ToString() + " and is up to date!");
				}
				catch (Exception ex)
				{
					Logging.Write(Colors.Teal,"Unable to run [ANIMUS Profile Updater] update process");
					Logging.Write(LogLevel.Diagnostic, "[ANIMUS Profile Updater]: Exception " + ex.Message);
				}
			}
		}

		public override void Dispose()
		{
			Logging.Write(Colors.Teal, "[ANIMUS] Deactivation Complete");
		}

		#endregion

		#region Privates/Publics
		
		private void needRestart()
		{
			if (brodieupdated)
				MessageBox.Show("Animus Plugin/Quest Behaviors have been updated, a restart is required.", "RESTART REQUIRED", MessageBoxButtons.OK, MessageBoxIcon.Information);
				
			var mainAnimusUI = new AnimusUI();
			mainAnimusUI.ShowDialog();
		}

		private static LocalPlayer Me { get { return StyxWoW.Me; } }
		
		#endregion

		#region Override Pulse
		public override void Pulse()
		{
			if (Me == null || !StyxWoW.IsInGame)
				return;
			if (Me.IsDead || Me.IsGhost)
				return;
			if (Battlegrounds.IsInsideBattleground || Me.IsInInstance || Me.IsOnTransport)
				return;
		}
		#endregion
	}
}
